//
//  MainTabBar.swift
//  Weibo
//
//  Created by 周先生 on 15/11/8.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {


    //默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //如果重写init 构造方法 系统就默认这个控件是由代码实现的
   required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }

    //添加撰写按钮
    private func setupUI() {
    
    addSubview(composeBtn)
    }
    override func layoutSubviews() {
        
        super.layoutSubviews()  //一定要调用父类的方法
         //手动修改所有按钮的frame
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRectMake(0, 0, w, h)
        //遍历所有的子视图

        //定义递增变量
        var index: CGFloat = 0
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            //修改frame
                subView.frame = CGRectOffset(rect, index * w, 0)
//                if index = 1 {
//                
//                index++
//                }
//                index++
                index += index == 1 ? 2 : 1
            }
        
        }
        //设置撰写按钮的位置
        composeBtn.frame = CGRectOffset(rect, 2 * w, 0)
        bringSubviewToFront(composeBtn)
    }
    //懒加载 撰写按钮
    lazy var composeBtn: UIButton = {
        //自定义按钮
        let btn = UIButton()
        //设置按钮
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        return btn
    }()
}
